package com.hulman.oms.service;

import com.hulman.oms.bean.Result;
import com.hulman.oms.bean.Area;
import com.hulman.oms.dao.AreaDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: maxwellens
 */
@Service
public class AreaService
{
    @Autowired
    private AreaDao areaDao;

    public List<Area> findAreas(Map<String, Object> map)
    {
        return areaDao.findAreas(map);
    }

    public Integer findAreasCount(Map<String, Object> map)
    {
        return areaDao.findAreasCount(map);
    }

    public Result findAreasResult(Map<String, Object> map)
    {
        Integer page = (Integer) map.get("page");
        Integer limit = (Integer) map.get("limit");
        if (page != null && limit != null)
        {
            map.put("start", (page - 1) * limit);
            map.put("length", limit);
        }
        int count = areaDao.findAreasCount(map);
        List<Area> data = areaDao.findAreas(map);
        return new Result(data, count);
    }

    public Area findAreaById(Integer id)
    {
        return areaDao.findAreaById(id);
    }

    public void saveArea(Area area)
    {
        if (area.getId() != null && area.getId() != 0)
        {
            areaDao.updateArea(area);
        } else
        {
            areaDao.insertArea(area);
        }
    }

    public void deleteAreaById(Integer id)
    {
        areaDao.deleteAreaById(id);
    }

    public void deleteAreas(int[] ids)
    {
        areaDao.deleteAreas(ids);
    }

}
